﻿using Furion.Logging.Extensions;
using HtmlAgilityPack;
using System.Net;
using System.Text;
using System.Xml;

namespace apitest.Application
{
    public class Spider1Service:IDynamicApiController
    {
        public  void Main( )
        {
            string url = "https://www.yixiangxws.com/14_14873/29381970.html";
            GetHtml(url);
        }

        public void GetHtml(string url)
        {
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            HtmlDocument doc = new HtmlDocument();
            string html = wc.DownloadString(url);
            doc.LoadHtml(html);
            var nodes = doc.DocumentNode;
            var title = nodes.SelectNodes("//h1").FirstOrDefault()?.InnerText;
            title.LogInformation();
            var contentList = nodes.SelectNodes("//*[@id=\"content\"]/p");
            string content = $"{title}\r\n";
            if (contentList!=null&&contentList.Count>0)
            {
                foreach (var item in contentList)
                {
                    var txt = item.InnerText.Replace("\r\n", "").Replace(" ", "");
                    content = content + txt + "\r\n";
                }
            }
            var next = nodes.SelectNodes("//*[@id=\"wrapper\"]/div[4]/div/div[2]/div[1]/a[3]").FirstOrDefault();
            var href = next.GetAttributes("href").FirstOrDefault().Value;
            href.LogInformation();
            href = "https:" + href;
            WriteTxt(content);
            if (title != "第633章 小疯子深渊发威，第一名斗法开始")
            {
                GetHtml(href);
            }
        }

        public void WriteTxt(string txt)
        {
            File.AppendAllText(@$"C:\123\aaa.txt", txt);
        }
    }
}
